﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using OpenLab.Model;

namespace OpenLab.DAL
{
	public partial class OrganismeDAL : OpenLab.DAL.IOrganismeDAL
	{
        public Organisme Add
			(Organisme organisme)
		{
				string sql ="INSERT INTO Organisme (designation, actif)  output inserted.id VALUES (@designation, @actif)";
				SqlParameter[] para = new SqlParameter[]
					{
						new SqlParameter("@designation", ToDBValue(organisme.designation)),
						new SqlParameter("@actif", ToDBValue(organisme.actif)),
					};
					
				int newId = (int)SqlHelper.ExecuteScalar(sql, para);
				return GetById(newId);
		}

        public int DeleteById(int id)
		{
            string sql = "DELETE Organisme WHERE Id = @Id";

           SqlParameter[] para = new SqlParameter[]
			{
				new SqlParameter("@id", id)
			};
		
            return SqlHelper.ExecuteNonQuery(sql, para);
		}
		
				
        public int Update(Organisme organisme)
        {
            string sql =
                "UPDATE Organisme " +
                "SET " +
			" designation = @designation" 
                +", actif = @actif" 
               
            +" WHERE id = @id";


			SqlParameter[] para = new SqlParameter[]
			{
				new SqlParameter("@id", organisme.id)
					,new SqlParameter("@designation", ToDBValue(organisme.designation))
					,new SqlParameter("@actif", ToDBValue(organisme.actif))
			};

			return SqlHelper.ExecuteNonQuery(sql, para);
        }		
		
        public Organisme GetById(int id)
        {
            string sql = "SELECT * FROM Organisme WHERE Id = @Id";
            using(SqlDataReader reader = SqlHelper.ExecuteDataReader(sql, new SqlParameter("@Id", id)))
			{
				if (reader.Read())
				{
					return ToModel(reader);
				}
				else
				{
					return null;
				}
       		}
        }
		
		public Organisme ToModel(SqlDataReader reader)
		{
			Organisme organisme = new Organisme();

			organisme.id = (int)ToModelValue(reader,"id");
			organisme.designation = (string)ToModelValue(reader,"designation");
			organisme.actif = (bool)ToModelValue(reader,"actif");
			return organisme;
		}
		
		public int GetTotalCount()
		{
			string sql = "SELECT count(*) FROM Organisme";
			return (int)SqlHelper.ExecuteScalar(sql);
		}
		
		public IEnumerable<Organisme> GetPagedData(int minrownum,int maxrownum)
		{
			string sql = "SELECT * from(SELECT *,row_number() over(order by id) rownum FROM Organisme) t where rownum>=@minrownum and rownum<=@maxrownum";
			using(SqlDataReader reader = SqlHelper.ExecuteDataReader(sql,
				new SqlParameter("@minrownum",minrownum),
				new SqlParameter("@maxrownum",maxrownum)))
			{
				return ToModels(reader);					
			}
		}
		
		public IEnumerable<Organisme> GetAll()
		{
			string sql = "SELECT * FROM Organisme";
			using(SqlDataReader reader = SqlHelper.ExecuteDataReader(sql))
			{
				return ToModels(reader);			
			}
		}
		
		protected IEnumerable<Organisme> ToModels(SqlDataReader reader)
		{
			var list = new List<Organisme>();
			while(reader.Read())
			{
				list.Add(ToModel(reader));
			}	
			return list;
		}		
		
		protected object ToDBValue(object value)
		{
			if(value==null)
			{
				return DBNull.Value;
			}
			else
			{
				return value;
			}
		}
		
		protected object ToModelValue(SqlDataReader reader,string columnName)
		{
			if(reader.IsDBNull(reader.GetOrdinal(columnName)))
			{
				return null;
			}
			else
			{
				return reader[columnName];
			}
		}
	}
}
